package  com.storage.control
{
	import com.firemoss.modelglue.controller.ModelGlueController;
	import com.firemoss.modelglue.event.ModelGlueEvent;
	import com.storage.business.CommonDelegate;
	import com.storage.business.ICommonDelegate;
	import com.storage.model.AppModelLocator;
	import com.storage.util.ExceptionHandler;
	import com.storage.view.widgets.form.ZZComBox;
	import com.storage.view.widgets.grid.ZZGrid;
	import com.storage.vo.query.QueryInfoVO;
	
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;

	public class CommonController extends ModelGlueController
	{
		public function CommonController()
		{
			super();
		}
		
		[Bindable]
		public var model:AppModelLocator = AppModelLocator.getInstance();
		
		public var service:ICommonDelegate = new CommonDelegate();
		
		
		//query for combobox data
		public function comboBoxQuery(e:ModelGlueEvent):void{
			model.showLoadingMask();
			var query:QueryInfoVO = e.getValue('query');
			var cbm:ZZComBox = e.getValue('comboBox') as ZZComBox;
			var name:String = e.getValue('name');
//			if(checkComboxLoad(name)==false){
//				model.removeMask();
//				return;
//			}
			service.findByQuery(query,function(list:ArrayCollection):void{
							if(name=='role'){
								model.roleList = list;
								cbm.dataProvider = model.roleList
							}else if(name=='category'){
								model.categoryList = list;
								cbm.dataProvider = model.categoryList;
							}else if(name=='size'){
								model.sizeList = list;			
								cbm.dataProvider = model.sizeList					
							}else if(name=='unit'){
								model.unitList = list;			
								cbm.dataProvider = model.unitList					
							}else if(name=='brand'){
								model.brandList = list;		
								cbm.dataProvider = model.brandList
							}else if(name=='department'){
								model.departmentList = list;
								cbm.dataProvider = model.departmentList
							}else if(name=='productCategory'){
								model.productCategory = list;
								cbm.dataProvider = model.productCategory
							}
							
							model.removeMask();
			},ExceptionHandler.onfault);
		}
		
		private function checkComboxLoad(name:String):Boolean{
					if(name=='role' && model.roleList.length==0 ){
						return true;
					}else if(name=='brand' && model.brandList.length==0 ){
						return true;
					}else if(name=='unit' && model.unitList.length==0 ){
						return true;
					}else if(name=='size' && model.sizeList.length==0 ){
						return true;
					}else if(name=='category' && model.categoryList.length==0 ){
						return true;
					}else if(name=='department' && model.departmentList.length==0 ){
						return true;
					}
			
			return false;
		}
		
		
	/* 	private function bindComboBoxData(grid:ZZGrid):void{
			var ay:ArrayCollection = grid.comboxList;
							for(var i:Number=0;i<ay.length;i++){
								var cmb:ZZComBox = ay[i] as ZZComBox;
								var pro:String = cmb.dataProviderName;
								if(pro==='category'){
									cmb.dataProvider = model.categoryList;
								}else if(pro==='size'){
									cmb.dataProvider = model.sizeList;
								}else if(pro==='unit'){
									cmb.dataProvider = model.unitList;
								}else if(pro==='brand'){
									cmb.dataProvider = model.brandList;
								}
							}
		} */
	
		public function searchByQuery(e:ModelGlueEvent):void{
			model.showLoadingMask();
			var query:QueryInfoVO = e.getValue('query');
			var grid:DataGrid ;
			if(e.exists('grid')){
				grid = e.getValue('grid') as DataGrid;
				grid.dataProvider = null;
			}
			var name:String = e.getValue('name');
			service.findByQuery(query,
								function(list:ArrayCollection):void{
									
									if(name==='permission'){
										model.permissionList = list;
										model.permissionList.refresh();
										if(grid!=null){
											grid.dataProvider = model.permissionList;
										}
										
									}else if(name==='user'){
										model.userList = list;
										model.userList.refresh();
										if(grid!=null){
											grid.dataProvider = model.userList;
										}
									}else if(name==='role'){
										model.roleList = list;
										model.roleList.refresh();
										if(grid!=null){
											grid.dataProvider = model.roleList;
										}
									}else if(name==='product'){
										model.productList = list;
										model.productList.refresh();
										if(grid!=null){
											grid.dataProvider = model.productList;	
										}
									}else if(name==='brand'){
										model.brandList = list;
										model.brandList.refresh();
										if(grid!=null){
											grid.dataProvider = model.brandList;	
										}
									}else if(name==='size'){
										model.sizeList = list;
										model.sizeList.refresh();
										if(grid!=null){
											grid.dataProvider = model.sizeList;	
										}
									}else if(name==='category'){
										model.categoryList = list;
										model.categoryList.refresh();
										if(grid!=null){
											grid.dataProvider = model.categoryList;	
										}
									}
//									else if(name==='comCategory'){
//										model.categoryComboBoxList = list;
//										model.categoryComboBoxList.refresh();
//										
//									}
									else if(name==='unit'){
										model.unitList = list;
										model.unitList.refresh();
										if(grid!=null){
											grid.dataProvider = model.unitList;	
										}
									}else if(name==='sell'){
										model.sellList = list;
										model.sellList.refresh();
										if(grid!=null){
											grid.dataProvider = model.sellList;	
										}
									}else if(name==='permission'){
										model.buyList = list;
										model.buyList.refresh();
										if(grid!=null){
											grid.dataProvider = model.buyList;	
										}
									}else if(name==='department'){
										model.departmentList = list;
										model.departmentList.refresh();
										if(grid!=null){
											grid.dataProvider = model.departmentList;	
										}
									}else if(name==='employee'){
										model.employeeList = list;
										
										if(grid!=null){
											grid.dataProvider = model.employeeList;	
										}
										model.employeeList.refresh();
									}else if(name==='customer'){
										model.customerList = list;
										model.customerList.refresh();
										if(grid!=null){
											grid.dataProvider = model.customerList;	
										}
									}else if(name==='buyProduct'){
										model.buyProductList= list;
										model.buyProductList.refresh();
										if(grid!=null){
											grid.dataProvider = model.buyProductList;	
										}
									}else if(name==='sellProduct'){
										model.sellProductList= list;
										model.sellProductList.refresh();
										if(grid!=null){
											grid.dataProvider = model.sellProductList;	
										}
									}else if(name==='buy'){
										model.buyList= list;
										model.buyList.refresh();
										if(grid!=null){
											grid.dataProvider = model.buyList;	
										}
									}
									model.removeMask();
									
								},
								
								ExceptionHandler.onfault);
				//setTimeout(function():void{model.removeMask();},1000*5);
			
		}
		
		public function loadAllForComboBox(e:ModelGlueEvent):void{
			/* model.showLoadingMask();
			var query:QueryInfoVO = e.getValue('query');
			var grid:ZZGrid ;
			if(e.exists('zzgrid')){
				grid = e.getValue('zzgrid') as ZZGrid;
			}
			var name:String = e.getValue('name');
			service.findByQuery(query,
						function(list:ArrayCollection):void{
							if(name=='role'){
								model.roleList = list;
								model.comboBoxDataDic['role'] = model.roleList;
							}else if(name=='category'){
								model.categoryList = list;
								model.comboBoxDataDic['category'] = model.categoryList;
							}else if(name=='size'){
								model.sizeList = list;
								model.comboBoxDataDic['size'] = model.sizeList;
								
							}else if(name=='unit'){
								model.unitList = list;
								model.comboBoxDataDic['unit'] = model.unitList;
								
							}else if(name=='brand'){
								model.brandList = list;
								model.comboBoxDataDic['brand'] = model.brandList;
		
							}else if(name=='department'){
								model.departmentList = list;
								model.comboBoxDataDic['department'] = model.departmentList;

							}
							
							model.removeMask();
							
						},
						
						ExceptionHandler.onfault); */
			
		} 
		
	
		
		
	}
}